package org.summer.lawyer.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 地址解析
 */
@Slf4j
public class AddressParseUtil {

    //省
    public static final String PROVINCE = "province";

    //市
    public static final String CITY = "city";

    //区
    public static final String DISTRICT = "District";

    //街道
    public static final String STREET = "street";

    //住宅地址
    public static final String RESIDENTIAL = "residential";

    //门牌号
    public static final String NUMBER = "number";

    //邮政编码
    public static final String POSTAL_CODE = "postalCode";

    //国家
    public static final String COUNTRY = "country";

    /**
     * 获取详细地址
     */
    public static String getDetailedAddress(String address) {
        String[] addressParts = spliceDetailedAddress(address);
        return addressParts[addressParts.length - 1];
    }

    /**
     * 获取省市区地址
     */
    public static String getProvinceCityDistrictAddress(String address){
        String[] addressParts = spliceDetailedAddress(address);
        StringBuilder builder = new StringBuilder();
        for(int i=0; i < addressParts.length - 1; i++){
            if(StrUtil.isNotEmpty(addressParts[i])){
                builder.append(addressParts[i]);
            }
        }
        return builder.toString();
    }


    /**
     * 详细地址拆分为【省、市、区、详细地址】
     */
    public static String[] spliceDetailedAddress(String address) {
        String[] addressParts = new String[4];
        try {
            String tempAddress = address;
            //省
            String province = null;
            int provinceIndex = parsingProvince(tempAddress);
            if (provinceIndex > -1) {
                province = tempAddress.substring(0, provinceIndex + 1);
                tempAddress = tempAddress.substring(provinceIndex + 1);
            }
            //市
            String city = null;
            int cityIdx = parsingCity(tempAddress);
            if (cityIdx > -1) {
                city = tempAddress.substring(0, cityIdx + 1);
                tempAddress = tempAddress.substring(cityIdx + 1);
            }
            //区
            String district = null;
            int districtIdx = parsingDistrict(tempAddress);
            if (districtIdx > -1) {
                district = tempAddress.substring(0, districtIdx + 1);
                tempAddress = tempAddress.substring(districtIdx + 1);
            }
            String street = tempAddress;
            addressParts[0] = province;
            addressParts[1] = city;
            addressParts[2] = district;
            addressParts[3] = street;
        } catch (Exception e) {
            log.error("解析[{}]地址信息发生错误：{}", address, e.getMessage());
        }
        return addressParts;
    }

    /**
     * 通过正则表达式提取地址
     */
    public static String getSimpleDetailedAddress(String address){
        if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
            address = address.substring(0, 3) + "市辖区" + address.substring(3);
        }
        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        while(m.find()){
            province = m.group("province");

            if (province.equals("北京市") || province.equals("天津市") || province.equals("上海市") || province.equals("重庆市")) {
                city = province;

                county = m.group("city");
                if (county.split("区").length > 1) {
                    town = county.substring(county.indexOf("区") + 1);
                    county = county.substring(0, county.indexOf("区") + 1);
                    if (town.contains("区")) {
                        town = town.substring(county.indexOf("区") + 1);
                    }
                } else {
                    county = m.group("county");
                    if (county != null && county.split("区").length > 1) {
                        town = county.substring(county.indexOf("区") + 1);
                        county = county.substring(0, county.indexOf("区") + 1);
                    }
                }
            } else {
                city = m.group("city");

                county = m.group("county");
                if (county != null && !"".equals(county)) {
                    if (county.split("市").length > 1 && county.indexOf("市") < 5) {
                        town = county;
                        county = county.substring(0, county.indexOf("市") + 1);
                        town = town.substring(county.indexOf("市") + 1);
                    }
                    if (county.split("旗").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("旗") + 1);
                        town = town.substring(county.indexOf("旗") + 1);
                    }
                    if (county.split("海域").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("海域") + 2);
                        town = town.substring(county.indexOf("海域") + 2);
                    }
                    if (county.split("区").length > 1) {
                        town = county;
                        county = county.substring(0, county.indexOf("区") + 1);
                        town = town.substring(county.indexOf("区") + 1);
                    }
                }

            }

            if (StrUtil.isNotEmpty(province)) {
                province = province + "-";
            }

            if (StrUtil.isNotEmpty(city)) {
                city = city + "-";
            }

            if (StrUtil.isNotEmpty(county)) {
                county = county + "-";
            }

            if (m.group("town") != null) {
                town += m.group("town");
            }

            if (StrUtil.isEmpty (county) && StrUtil.isNotEmpty(town)) {
                town = town + "-";
            }
            village=m.group("village");

        }

        String newMachineAdress = province + city + county + town + village;
        if (StrUtil.isNotEmpty(newMachineAdress)) {
            newMachineAdress = newMachineAdress.replaceAll("null", "");
        }

        if (StrUtil.isEmpty(newMachineAdress)) {
            newMachineAdress = address;
        }

        return newMachineAdress;
    }

    /**
     * 解析地址中的省、市信息
     */
    private static int parsingProvince(String address) {
        int[] idx = new int[3];
        int provinceIndex = -1;
        if ((provinceIndex = address.indexOf("省")) > -1) {
            idx[0] = provinceIndex;
        }
        if ((provinceIndex = address.indexOf("市")) > -1) {
            idx[1] = provinceIndex;
        }
        if ((provinceIndex = address.indexOf("区")) > -1) {
            idx[2] = provinceIndex;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return provinceIndex;
    }

    /**
     * 解析地址中的市区、县、自治州
     */
    private static int parsingCity(String address) {
        int[] idx = new int[7];
        int cityIndex = -1;
        if ((cityIndex = address.indexOf("县")) > -1) {
            idx[0] = cityIndex;
        }
        if ((cityIndex = address.indexOf("自治州")) > -1) {
            idx[1] = cityIndex + 2;
        }
        if ((cityIndex = address.indexOf("市辖区")) > -1) {
            idx[2] = cityIndex + 2;
        }
        if ((cityIndex = address.indexOf("市")) > -1) {
            idx[3] = cityIndex;
        }
        if ((cityIndex = address.indexOf("区")) > -1) {
            idx[4] = cityIndex;
        }
        if ((cityIndex = address.indexOf("地区")) > -1) {
            idx[5] = cityIndex + 1;
        }
        if ((cityIndex = address.indexOf("盟")) > -1) {
            idx[6] = cityIndex;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return cityIndex;
    }

    /**
     * 解析地址中的县、旗、岛信息
     */
    private static int parsingDistrict(String address) {
        int[] idx = new int[6];
        int districtIndex = -1;
        if ((districtIndex = address.indexOf("县")) > -1) {
            idx[0] = districtIndex;
        }
        if ((districtIndex = address.indexOf("旗")) > -1) {
            idx[1] = districtIndex;
        }
        if ((districtIndex = address.indexOf("海域")) > -1) {
            idx[2] = districtIndex + 1;
        }
        if ((districtIndex = address.indexOf("市")) > -1) {
            idx[3] = districtIndex;
        }
        if ((districtIndex = address.indexOf("区")) > -1) {
            idx[4] = districtIndex;
        }
        if ((districtIndex = address.indexOf("岛")) > -1) {
            idx[5] = districtIndex;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return districtIndex;
    }

}
